function AssignObjects(fin, fout)

load(fin);

[vareas, veas, vebs, vephis, vex0s, vey0s, vx1s, vx2s, vy1s, vy2s, vxheights, vyheights, objs] = ShiftIndexes(vareas, veas, vebs, vephis, vex0s, vey0s, vx1s, vx2s, vy1s, vy2s, vxheights, vyheights, objs, nfindobjs);

len = length(objs);

binit = 0;

x1s = zeros(len, nfindobjs);
x2s = zeros(len, nfindobjs);
y1s = zeros(len, nfindobjs);
y2s = zeros(len, nfindobjs);

x0s = zeros(len, nfindobjs);
y0s = zeros(len, nfindobjs);

ex0s = zeros(len, nfindobjs);
ey0s = zeros(len, nfindobjs);

eas = zeros(len, nfindobjs);
ebs = zeros(len, nfindobjs);
ets = zeros(len, nfindobjs);
ephis = zeros(len, nfindobjs);

xheights = zeros(len, nfindobjs);
yheights = zeros(len, nfindobjs);
areas = zeros(len, nfindobjs);

maxmissframes = 5;

prevx0s = zeros(nfindobjs, 1);
prevy0s = zeros(nfindobjs, 1);
prevlens = zeros(nfindobjs, 1);
nmissframes = zeros(nfindobjs, 1);
merged = zeros(len, nfindobjs);
bmerged = zeros(len, 1);

a = find(objs == nfindobjs);
meana = mean(vebs(a,1));
meanb = mean(vebs(a,1));

d = sqrt(meana^2+meanb^2)*4.0;

minmaxdist = d; 
ninitobjs = 0;

i = 1;

while i <= len
 
    index = i;
    
    nobjs = 0;
    for j = 1:nfindobjs
        if vx1s(index,j) > 0
            nobjs = nobjs + 1;
        end
    end
    
    objs(index) = nobjs;
         
    
    if objs(index) == 0 
        binit =0;
    elseif index > 1 & objs(index) <= objs(index-1)   
        
        closestdist = 1000000;
        for j = 1:nfindobjs
            for k = 1:nfindobjs                
                if j ~= k & x0s(index, j) > 0 & x0s(index, k) > 0
                    
                    xj = x0s(index, j);
                    yj = y0s(index, j);
                    xk = x0s(index, k);
                    yk = y0s(index, k);
                    
                    dx = xj-xk;
                    dy = yj-yk;
                    
                    d = sqrt(dx^2+dy*dy);
                
                    if d < closestdist
                        closestdist = d;
                    end
                end
            end
        end
        
        if closestdist < minmaxdist
            binit = 0;
        end        
    end

    if binit == 0 & objs(i) > 0                   
         
            for j = 1:nfindobjs
                x1s(index,j) = vx1s(index, j);
                x2s(index,j) = vx2s(index, j);
                y1s(index,j) = vy1s(index, j);
                y2s(index,j) = vy2s(index, j);                
                                
                x1 = x1s(index,j);
                x2 = x2s(index,j);
                y1 = y1s(index,j);
                y2 = y2s(index,j);
                
                x0s(index,j) = (abs(x2-x1)/2.0)+x1;
                y0s(index,j) = (abs(y2-y1)/2.0)+y1;                     
                
                
                ex0s(index,j) = vex0s(index, j);
                ey0s(index,j) = vey0s(index, j);
                
                eas(index,j) = veas(index, j);
                ebs(index,j) = vebs(index, j);                                
                
                ets(index,j) = vets(index, j);
                
                ephis(index,j) = vephis(index, j);                
                
                xheights(index, j) = vxheights(index, j);
                yheights(index, j) = vyheights(index, j);
                areas(index, j) = vareas(index, j);
                
                prevx0s(j) = ex0s(index,j);
                prevy0s(j) = ey0s(index,j);
                
                a = veas(index, j);
                b = vebs(index, j);
                
                prevlens(j) = sqrt(a^2+b^2);
                
                nmissframes = zeros(nfindobjs, 1);
            end
            
            ninitobjs = objs(i);
            binit = 1;

            
     elseif binit == 1 & objs(i) > 0                        
                        
            nmissed = zeros(nfindobjs, 1);
            
            for j = 1:nfindobjs                                
                if vex0s(index,j) == 0
                    nmissed(j) = 1;                                    
                end                                                                             
            end
            
             if index < len - maxmissframes
                 bchangeobjcount = 1;
                 for j = 1:maxmissframes
                    if objs(index+j) == ninitobjs
                        bchangeobjcount = 0;
                    end
                 end                                  
                 
                 if bchangeobjcount == 1
                     binit = 0;
                 end
             end
           
            [adists, assignments, bassigned] = AssignUniqueObjDists(objs(i), ninitobjs, vex0s(index,:), vey0s(index,:), prevx0s, prevy0s);
            
            if bassigned == 0
				for j = 1:nfindobjs
					a = find(assignments == j);
					
					if length(a) > 1
						merged(index, a) = 1;
						bmerged(index) = 1;
					end
				end
            end
          
                      
            ncountobjs = 0;           

            for j = 1:ninitobjs
                jcurrent = assignments(j);
                
                dist = adists(j);
                
                a = veas(index, jcurrent);
                b = vebs(index, jcurrent);
                                
                maxdist = max([sqrt(a^2+b^2), prevlens(j)]);
                
                if maxdist < minmaxdist
                    maxdist = minmaxdist;
                end                
                
                if dist < maxdist    
					x1s(index,j) = vx1s(index, jcurrent);
					x2s(index,j) = vx2s(index, jcurrent);
					y1s(index,j) = vy1s(index, jcurrent);
					y2s(index,j) = vy2s(index, jcurrent);                
                                
					x1 = x1s(index,jcurrent);
					x2 = x2s(index,jcurrent);
					y1 = y1s(index,jcurrent);
					y2 = y2s(index,jcurrent);
                
					x0s(index,j) = (abs(x2-x1)/2.0)+x1;
					y0s(index,j) = (abs(y2-y1)/2.0)+y1;                 
                
					ex0s(index,j) = vex0s(index, jcurrent);
					ey0s(index,j) = vey0s(index, jcurrent);
                
					eas(index,j) = veas(index, jcurrent);
					ebs(index,j) = vebs(index, jcurrent);
                    
					ets(index,j) = vets(index, jcurrent);
                
					ephis(index,j) = vephis(index, jcurrent);
                    
                    
					xheights(index, j) = vxheights(index, jcurrent);
					yheights(index, j) = vyheights(index, jcurrent);
					areas(index, j) = vareas(index, jcurrent);                                        
                    
                    a = veas(index, jcurrent);
                    b = vebs(index, jcurrent);
                
                    prevx0s(j) = vex0s(index,jcurrent);
                    prevy0s(j) = vey0s(index,jcurrent);
                    prevlens(j) = sqrt(a^2+b^2);      
                    
                    nmissframes(j) = 0;
                    
                    ncountobjs = ncountobjs + 1;
                else
                    nmissed(j) = 1;
				end
            end    
            
            nmissframes = nmissframes + nmissed;
  
            nobjs = objs(i);
            objs(i) = ncountobjs;
            m = min(nmissframes);
            
            
            if m > maxmissframes
                binit = 0;
                i = i + 1;                                               
            elseif nobjs == ninitobjs & ncountobjs < ninitobjs & max(nmissframes) > maxmissframes                
                binit = 0;                
                i = i + 1;               
            end
                  
    else
        binit = 0;
    end
    
    i = i + 1;
end




for i = 1:len
    
    nobjs = 0;
    
    bassigned = zeros(nfindobjs, 1);
    
    for j = 1:nfindobjs
        bunique = 1;
        
        if x1s(i,j) > 0
            
            for k = 1:nfindobjs
                if j ~= k
                    if x1s(i,j) == x1s(i,k) & x2s(i,j) == x2s(i,k) &...
                       y1s(i,j) == y1s(i,k) & y2s(i,j) == y2s(i,k) & (bassigned(j) == 1 || bassigned(k) == 1)
                          bunique = 0;
                    end
                end
            end
        else
            bunique = 0;
        end
        
        if bunique == 1
            nobjs = nobjs + 1;
            
            bassigned(j) = 1;
        end
    end
    
    objs(i) = nobjs;
    
        
end


objs1 = zeros(len,1);
bsegmented = 0;

nfindobjs
x = find(objs == nfindobjs);
z = find(objs >= 2);
y = find(objs > 0);
length(x)/length(objs)
length(z)/length(objs)
length(y)/length(objs)




save(fout, 'objs', 'x1s', 'x2s', 'y1s', 'y2s', 'ex0s', 'ey0s', 'ephis', 'eas', 'ebs', 'ets',...    
    'xheights', 'yheights', 'areas', 'nprocessstart', 'imgx', 'imgy', 'imghalfx', 'imghalfy',...
    'nfindobjs', 'nfindmaxobjs', 'nstartframe', 'nendframe', 'objs1', 'bsegmented', 'merged', 'bmerged');





